﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Tips
{
    public class Tips
    {
        public static void InsertTip(Model.Tip tip, ref Model.NutriLeafDBEntities context)
        {
            context.Tips.AddObject(tip);
            context.SaveChanges();
        }

        public static void InsertTip(Model.Tip tip, Model.TipCategory category, ref Model.NutriLeafDBEntities context)
        {
            tip.TipCategory = category;
            context.Tips.AddObject(tip);
            context.SaveChanges();
        }

        public static void InsertTip(Model.Tip tip, int tipCategoryId, ref Model.NutriLeafDBEntities context)
        {
            tip.TipCategoryID = tipCategoryId;
            context.Tips.AddObject(tip);
            context.SaveChanges();
        }

        public static void InsertTip(Model.Tip tip, Model.TipCategory category, int userId, ref Model.NutriLeafDBEntities context)
        {
            tip.TipCategory = category;
            tip.WrittenByUserID = userId;
            context.Tips.AddObject(tip);
            context.SaveChanges();
        }

        public static void ApproveTip(Model.Tip tip, ref Model.NutriLeafDBEntities context)
        {
            Model.Tip t = context.Tips.Where(p => p.TipID == tip.TipID).SingleOrDefault();
            t.Approved = true;
            context.SaveChanges();

        }

        public static void ApproveTip(List<Model.Tip> tip, ref Model.NutriLeafDBEntities context)
        {
            foreach (Model.Tip t in tip) {
                Model.Tip sample = context.Tips.Where(p => p.TipID == t.TipID).First();
                sample.Approved = true;
            }
            context.SaveChanges();
        }

        public static void InsertTipCategory(Model.TipCategory category, ref Model.NutriLeafDBEntities context)
        {
            context.TipCategories.AddObject(category);
            context.SaveChanges();
        }

        public static void InsertTipCategegory(Model.TipCategory category, Model.TipCategory parentCategory, ref Model.NutriLeafDBEntities context)
        {
            
        }

        public static void InsertTipCategory(Model.TipCategory category, int parentCategoryId, ref Model.NutriLeafDBEntities context)
        {
            
        }

        public static void InsertTipCategory(string categoryName, string categoryDescription, int ParentCategoryID, ref Model.NutriLeafDBEntities context)
        {
            Model.TipCategory cat = new Model.TipCategory();
            cat.CategoryDescription = categoryDescription;
            cat.CategoryName = categoryName;
            cat.ParentCategoryID = ParentCategoryID;
            context.TipCategories.AddObject(cat);
            context.SaveChanges();
        }

        public static void InsertTipCategory(string categoryName, string categoryDescription, ref Model.NutriLeafDBEntities context)
        {
            Model.TipCategory cat = new Model.TipCategory();
            cat.CategoryDescription = categoryDescription;
            cat.CategoryName = categoryName;
         
            context.TipCategories.AddObject(cat);
            context.SaveChanges();
        }

        public static void InsertTipViewed(Model.TipViewed viewed, ref Model.NutriLeafDBEntities context)
        {
            context.TipVieweds.AddObject(viewed);
            context.SaveChanges();
        }

        public static void InsertTipViewed(Model.Tip tip, Model.User user, ref Model.NutriLeafDBEntities context)
        {
            Model.TipViewed tp = new Model.TipViewed();
            tp.User = user;
            context.TipVieweds.AddObject(tp);
            context.SaveChanges();
        }

        public static void InsertTipViewed(int tipId, int userId, ref Model.NutriLeafDBEntities context)
        {
            Model.TipViewed tp = new Model.TipViewed();
            tp.UserID = userId;
            context.TipVieweds.AddObject(tp);
            context.SaveChanges();

        }

        public static bool IsTipApproved(int tipId, ref Model.NutriLeafDBEntities context)
        {
            return context.Tips.Where(p => p.TipID == tipId).First().Approved.Value;
        }

        public static bool IsTipApproved(Model.Tip tip, ref Model.NutriLeafDBEntities context)
        {
            return context.Tips.Where(p => p.TipID ==tip.TipID).First().Approved.Value;
        }

        public static List<Model.Tip> GetAllNotApprovedTips(Model.TipCategory category, ref Model.NutriLeafDBEntities context)
        {
            Model.TipCategory categories = context.TipCategories.Where(p => p.TipCategoryID == category.TipCategoryID).First();
            return categories.Tips.Where(p => p.Approved == false).ToList();
        }

        public static List<Model.Tip> GetAllNotApprovedTips(int categoryId, ref Model.NutriLeafDBEntities context)
        {
            Model.TipCategory categories = context.TipCategories.Where(p => p.TipCategoryID == categoryId).First();
            return categories.Tips.Where(p => p.Approved == false).ToList();
        }

        public static List<Model.Tip> GetAllNotApprovedTips(ref Model.NutriLeafDBEntities context)
        {
            return context.Tips.Where(p => p.Approved == false).ToList();
        }

        public static Model.Tip GetRandomTip( ref Model.NutriLeafDBEntities context)
        {
            Random r = new Random(0);
           int id= r.Next(0, context.Tips.Count());
           return context.Tips.Where(p => p.TipID == id).First();
        }

        public static Model.Tip GetRandomTip( int categoryId, ref Model.NutriLeafDBEntities context)
        {
              Model.TipCategory categories = context.TipCategories.Where(p => p.TipCategoryID == categoryId).First();
              Random r = new Random();
              int id = r.Next(0, categories.Tips.Count);
              return context.Tips.Where(p => p.TipID == id).First();

             
        }

        public static Model.Tip GetRandomTip(Model.TipCategory category, ref Model.NutriLeafDBEntities context)
        {
            Model.TipCategory categories = context.TipCategories.Where(p => p.TipCategoryID ==category.TipCategoryID).First();
            Random r = new Random();
            int id = r.Next(0, categories.Tips.Count);
            return context.Tips.Where(p => p.TipID == id).First();

        }

        public static void RemoveTip(Model.Tip tip, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.Tips.Where(p => p.TipID == tip.TipID).First());
            context.SaveChanges();
        }

        public static void MoveTipsToNewCategory(List<Model.Tip> tips, Model.TipCategory newCategory, ref Model.NutriLeafDBEntities context)
        {
          
            Model.TipCategory  cat= context.TipCategories.Where(p => p.TipCategoryID == newCategory.TipCategoryID).First();
            foreach (Model.Tip t in tips) {
                cat.Tips.Add(t);
                
            }
         
            context.SaveChanges();
        }

        public static void MoveTipsToNewCategory(Model.TipCategory oldCateogry, Model.TipCategory newCategory, ref Model.NutriLeafDBEntities context)
        {

            Model.TipCategory cat = context.TipCategories.Where(p => p.TipCategoryID == oldCateogry.TipCategoryID).First();
            Model.TipCategory newcategory = context.TipCategories.Where(p => p.TipCategoryID == newCategory.TipCategoryID).First();
            foreach (Model.Tip t in cat.Tips)
            {
                newcategory.Tips.Add(t);

            }

            context.SaveChanges();
        }

        public static void RemoveCategory(Model.TipCategory category, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.TipCategories.Where(p => p.TipCategoryID == category.TipCategoryID).First());
            context.SaveChanges();
        }

        public static void RemoveCategory(int categoryId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.TipCategories.Where(p => p.TipCategoryID == categoryId).First());
            context.SaveChanges();
        }

        public static void LikeTip(int tipId, int userId, bool like, ref Model.NutriLeafDBEntities context)
        {
          Model.Tip tip= context.Tips.Where(p => p.TipID == tipId && p.User.UserID == userId).First();
          Model.TipsLike likes = new Model.TipsLike();
          likes.TipID = tipId;
          likes.UserID = userId;
          likes.Liked = like;
          tip.TipsLikes.Add(likes);
          context.SaveChanges();
        }

        public static void LikeTip(Model.Tip tip, Model.User user, bool like, ref Model.NutriLeafDBEntities context)
        {
            Model.TipsLike likes = context.TipsLikes.Where(p => p.UserID == user.UserID && p.TipID == tip.TipID).First();
            likes.Liked = like;
            context.SaveChanges();
        }

        public static void RemoveTipLike(Model.TipsLike tipsLike, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.TipsLikes.Where(p => p.TipLikeID == tipsLike.TipLikeID).First());
            context.SaveChanges();
        }

        public static void RemoveTipViewed(Model.TipViewed tipViewed, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.TipVieweds.Where(p => p.TipViewedID == tipViewed.TipViewedID).First());
            context.SaveChanges();
        }

        public static void UpdateTip(Model.Tip tip, ref Model.NutriLeafDBEntities context)
        {
          Model.Tip t=  context.Tips.Where(p => p.TipID == tip.TipID).First();
          int id = tip.TipID;
            t = tip;
            t.TipID = id;
          context.SaveChanges();

  
        }

        public static void UpdateTipCategory(Model.TipCategory category, ref Model.NutriLeafDBEntities context)
        {
            Model.TipCategory categories = context.TipCategories.Where(p => p.TipCategoryID == category.TipCategoryID).First();
            int id = categories.TipCategoryID;
            categories = category;
            categories.TipCategoryID = id;
            context.SaveChanges();
          
        }

        public static void UpdateTipLike(Model.TipsLike tipLike, ref Model.NutriLeafDBEntities context)
        {
           Model.TipsLike like= context.TipsLikes.Where(p => p.TipLikeID == tipLike.TipLikeID).First();
           int id = like.TipLikeID;
           like = tipLike;
           like.TipLikeID = id;
           context.SaveChanges();

        }

        public static void UpdateTipViewed(Model.TipViewed viewed, ref Model.NutriLeafDBEntities context)
        {
            Model.TipViewed like = context.TipVieweds.Where(p => p.TipViewedID == viewed.TipViewedID).First();
            int id = like.TipViewedID;
            like = viewed;
            like.TipViewedID = id;
            context.SaveChanges();
        }

        public static void UpdateUserTipCategory(Model.UserTipCategory category, ref Model.NutriLeafDBEntities context)
        {

            Model.UserTipCategory like = context.UserTipCategories.Where(p => p.TipCategoryID ==category.TipCategoryID).First();
            int id = like.TipCategoryID;
            like = category;
            like.TipCategoryID= id;
            context.SaveChanges();
        }

        public static void AddUserTipCategory(int tipCategoryId, int userId, ref Model.NutriLeafDBEntities context)
        {
            Model.UserTipCategory cat = new Model.UserTipCategory();
            cat.TipCategoryID = tipCategoryId;
            cat.UserID = userId;
            context.UserTipCategories.AddObject(cat);
            context.SaveChanges();
        }

        public static void AddUserTipCategory(Model.UserTipCategory category, ref Model.NutriLeafDBEntities context)
        {
            context.UserTipCategories.AddObject(category);
            context.SaveChanges();
        }

        public static void RemoveUserTipCategory(Model.UserTipCategory category, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.UserTipCategories.Where(p=>category.UserTipCategoryID == category.UserTipCategoryID).First());
            context.SaveChanges();
        }

        public static void RemoveUserTipCategory(int userId, int tipCategoryId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.UserTipCategories.Where(p => p.UserID == userId && p.TipCategoryID == tipCategoryId).First());
            context.SaveChanges();
        }

        public static List<Model.Tip> GetTipsByCategory(int categoryId, ref Model.NutriLeafDBEntities context)
        {


            return context.Tips.Where(p => p.TipCategoryID == categoryId).ToList();
          
        }

        public static List<Model.Tip> GetTipsByCategory(Model.TipCategory category, ref Model.NutriLeafDBEntities context)
        {

            return context.Tips.Where(p => p.TipCategoryID == category.TipCategoryID).ToList();
          
        }

        public static List<Model.Tip> GetTipsLikedByUser(int userId, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Tip> t = new List<Model.Tip>();
           List<Model.TipsLike> c= context.TipsLikes.Where(p => p.Liked == true && p.UserID == userId).ToList();
           foreach (Model.TipsLike l in c) {
               t.Add(l.Tip);

           }
           return t;
        }

        public static List<Model.TipCategory> GetParentCategories(ref Model.NutriLeafDBEntities context)
        {
            return (from a in context.TipCategories
                    where a.ParentCategoryID == null
                    select a).ToList();
        }

        public static List<Model.TipCategory> GetSubCategories(int tipParentCategory,ref Model.NutriLeafDBEntities context)
        {
            return (from a in context.TipCategories
                    where a.ParentCategoryID == tipParentCategory
                    select a).ToList();
        }

        public static List<Model.Tip> GetMostPopularTips(int categoryId,int count, int skip, ref Model.NutriLeafDBEntities context)
        {
            if (categoryId == 0)
            {
                return (from a in context.Tips
                        orderby a.TipsLikes.Count descending
                        select a).Skip(skip).Take(count).ToList();
            }
            else
            {
                return (from a in context.Tips
                        where a.TipCategoryID==categoryId
                        orderby a.TipsLikes.Count descending
                        select a).Skip(skip).Take(count).ToList();
            }
        }

        public static List<Model.TipCategory> GetAllTipCategory(ref Model.NutriLeafDBEntities context)
        {
            return (from a in context.TipCategories
                    select a).ToList();
        }
    }
}